﻿using System.Collections.Generic;
using System.Linq;
using Diploma.Models.IRepositories;

namespace Diploma.Models.Repositories
{
    internal class LecturersLoadingsRepository : ILecturersLoadingsRepository
    {
        private readonly DiplomaEntities dataContext;

        public LecturersLoadingsRepository(DiplomaEntities dataContext)
        {
            this.dataContext = dataContext;
        }


        public IEnumerable<LecturersLoading> GetLoadingsForLecturer(Lecturer lecturer)
        {
            List<QualificationLevel> levels = dataContext.QualificationLevels.ToList();
            List<LecturersLoading> loadings = lecturer.LecturersLoadings.Where(l => l.DeletionDate == null).ToList();

            if (loadings.Count() == levels.Count())
                return lecturer.LecturersLoadings;

            foreach (QualificationLevel level in levels)
            {
                if (loadings.Where(l => l.QualificationLevelId == level.QualificationLevelId).Count() == 0)
                {
                    var newLoading = new LecturersLoading
                                         {
                                             Lecturer = lecturer,
                                             StudentsAmount = 0,
                                             QualificationLevel = level,
                                         };

                    dataContext.AddToLecturersLoadings(newLoading);
                }
            }

            dataContext.SaveChanges();

            return lecturer.LecturersLoadings;
        }
    }
}